﻿#include "dialogset.h"
#include "ui_dialogset.h"
#include <QSettings>


#define AUTO_RUN "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"

DialogSet::DialogSet(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogSet)
{
    ui->setupUi(this);

    QString application_name = QApplication::applicationName();//获取应用名称
    QSettings *settings = new QSettings(AUTO_RUN, QSettings::NativeFormat);//创建QSetting, 需要添加QSetting头文件
    QString application_path = QApplication::applicationFilePath();//找到应用的目录
    QString autoRunStr =  settings->value( application_name).toString();//写入注册表
    qDebug()<<application_path.replace("/", "\\") ;
    if(application_path.replace("/", "\\") ==autoRunStr)
    {
        ui->checkBox->setChecked(true);
    }
    else{
        ui->checkBox->setChecked(false);
    }
}


DialogSet::~DialogSet()
{
    delete ui;
}


void DialogSet::SetMyAppAutoRun(bool isstart)
{
    QString application_name = QApplication::applicationName();//获取应用名称
    QSettings *settings = new QSettings(AUTO_RUN, QSettings::NativeFormat);//创建QSetting, 需要添加QSetting头文件
    if(isstart){
        QString application_path = QApplication::applicationFilePath();//找到应用的目录
        settings->setValue(application_name, application_path.replace("/", "\\"));//写入注册表
    }
    else {settings->remove(application_name);}
}



void DialogSet::on_pushButton_clicked()
{

    if(ui->checkBox->isChecked()){
        SetMyAppAutoRun(true);
    }
    else
    {
        SetMyAppAutoRun(false);
    }
    this->close();
}

